package com.coocaa.ops.admin.v1.module.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coocaa.ops.admin.v1.module.convert.ModuleSaveConverter;
import com.coocaa.ops.admin.v1.module.mapper.IModuleDefaultOnclickMapper;
import com.coocaa.ops.common.core.biz.module.constant.ModuleTypeConstants;
import com.coocaa.ops.common.core.biz.module.entity.ModuleInfoEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author tangminghong
 * @date 2020-07-24
 */
@Component
public class ModuleOnclickUtil {

    private static IModuleDefaultOnclickMapper moduleDefaultOnclickMapper;

    @Autowired
    public ModuleOnclickUtil(IModuleDefaultOnclickMapper moduleDefaultOnclickMapper) {
        ModuleOnclickUtil.moduleDefaultOnclickMapper = moduleDefaultOnclickMapper;
    }

    public static String getFinalOnclick(ModuleInfoEntity moduleEntity) {
        String onclick = moduleEntity.getOnclick();
        if (StringUtils.isBlank(onclick)) {
            //点击事件是空，取默认
            moduleEntity.setOnclickDefault(1);
            return getDefaultOnclick(moduleEntity);
        }
        moduleEntity.setOnclickDefault(0);
        return moduleEntity.getOnclick();
    }

    private static String getDefaultOnclick(ModuleInfoEntity moduleEntity) {
        String moduleType = moduleEntity.getModuleType();
        if (moduleType == null) { return "{}";}
        String onclick = moduleDefaultOnclickMapper.getDefaultOnclick(moduleType);
        if (StringUtils.isNotBlank(onclick)) {
            return onclick;
        }
        switch (moduleType) {
            /*case ModuleTypeConstants.TODAY:
                onclick = "{" +
                        "\"dowhat\":\"startActivity\"," +
                        "\"packagename\":\"com.ccos.tvlauncher\"," +
                        "\"bywhat\":\"action\"," +
                        "\"byvalue\":\"ccos.action.HOME.Today\"," +
                        "\"versioncode\":\"-1\"" +
                        "}";
                break;*/
            case ModuleTypeConstants.MOVIE:
                onclick = "{" +
                        "\"dowhat\":\"startActivity\"," +
                        "\"packagename\" : \"com.tianci.movieplatform\"," +
                        "\"bywhat\":\"action\"," +
                        "\"byvalue\":\"coocaa.intent.action.HOME\"," +
                        "\"versioncode\":\"-1\"" +
                        "}";
                break;
            case ModuleTypeConstants.SHORT_VIDEO:
                onclick = "{" +
                        "\"dowhat\":\"startActivity\"," +
                        "\"packagename\":\"com.cheese.tv.yst\"," +
                        "\"bywhat\":\"action\"," +
                        "\"byvalue\":\"coocaa.intent.action.U14.Splash\"," +
                        "\"versioncode\":\"-1\"" +
                        "}";
                break;
            case ModuleTypeConstants.AIOT:
                onclick = "{" +
                        "\"dowhat\":\"startActivity\"," +
                        "\"packagename\":\"com.skyworth.smarthome_tv\"," +
                        "\"bywhat\":\"action\"," +
                        "\"byvalue\":\"com.smarthome.action.HOME\"," +
                        "\"versioncode\":\"-1\"" +
                        "}";
                break;
            case ModuleTypeConstants.APP_STORE:
                onclick = "{" +
                        "\"dowhat\":\"startActivity\"," +
                        "\"packagename\":\"com.tianci.appstore\"," +
                        "\"bywhat\" : \"action\"," +
                        "\"byvalue\" : \"coocaa.intent.action.APP_STORE4_HOME\"," +
                        "\"versioncode\":\"-1\"" +
                        "}";
                break;
            case ModuleTypeConstants.EDUCATE:
                onclick = "{" +
                        "\"dowhat\":\"startActivity\"," +
                        "\"packagename\":\"com.coocaa.educate\"," +
                        "\"bywhat\":\"action\"," +
                        "\"byvalue\":\"coocaa.intent.action.HOME_EDU\"," +
                        "\"versioncode\":\"-1\"" +
                        "}";
                break;
            case ModuleTypeConstants.SHOPPING_MALL:
                onclick = "{" +
                        "\"dowhat\":\"startActivity\"," +
                        "\"packagename\":\"com.coocaa.mall\"," +
                        "\"bywhat\":\"action\"," +
                        "\"byvalue\":\"coocaa.intent.action.MALL_HOME\"," +
                        "\"versioncode\":\"-1\"," +
                        "\"params\":{" +
                        "\"tabId\":\"104961\"" +
                        "}}";
                break;
            case ModuleTypeConstants.SYSTEM:
                onclick = "{" +
                        "\"dowhat\":\"startActivity\"," +
                        "\"packagename\":\"com.tianci.setting\"," +
                        "\"bywhat\":\"action\"," +
                        "\"byvalue\":\"android.settings.SETTINGS\"," +
                        "\"versioncode\":\"-1\"" +
                        "}";
                break;
            case ModuleTypeConstants.MUSIC:
                onclick = "{" +
                        "\"dowhat\":\"startActivity\"," +
                        "\"packagename\":\"com.coocaa.karaoke\"," +
                        "\"bywhat\":\"action\"," +
                        "\"byvalue\":\"coocaa.intent.music.ACTION_HOME\"," +
                        "\"versioncode\":\"-1\"" +
                        "}";
                break;
            default:
                onclick = "{}";
        }
        return onclick;
    }

    private static boolean allFieldsEmpty(String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        boolean allFieldsEmpty = true;
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            Object value = entry.getValue();
            if (value == null) { continue; }
            if (value instanceof JSONObject) {
                if (!((JSONObject)value).isEmpty()) {allFieldsEmpty = false; break;}
            } else if (value instanceof JSONArray) {
                if (!((JSONArray)value).isEmpty()) {allFieldsEmpty = false; break;}
            } else {
                if (!value.toString().isEmpty()) {allFieldsEmpty = false; break;}
            }
        }
        return allFieldsEmpty;
    }
}
